'use strict';

/**
 * @ngdoc directive
 * @name jgappApp.directive:sheetAction
 * @description
 * # sheetAction
 */
angular.module('jgappApp')
    .directive('sheetAction', ['$rootScope', '$location', function($rootScope, $location) {
        return {
            scope: {
                sheetObj: "=sheetData"
            },
            template: '<div class="sheetAction">' +
                '<div class="sheet-top flex">' +
                '<div class="sheet-cancel tl c9" ng-click="sheetCancel()">取消</div>' +
                '<div class="sheet-title tc">{{sheetObj.sheettitle}}</div>' +
                '<div class="sheet-confirm tr" ng-click="sheetConfirm()">确定</div>' +
                '</div>' +
                '<div class="sheet-list tc">' +
                '<div class="sheet-item" ng-click="onClickTab($index)" ng-class="{active:isActiveTab($index)}" ng-repeat="item in sheetObj.tabList">{{item.key}}</div>' +
                '</div>' +
                '</div>',
            restrict: 'E',
            link: function postLink(scope, element, attrs) {
                scope.onClickTab = function(index) {
                    if (scope.currentTab != index) {
                        scope.currentTab = index;
                    }
                };
                scope.isActiveTab = function(index) {
                    return index == scope.currentTab;
                };
                scope.sheetCancel = function() {
                    closeSheet()
                        // 通过当前选项设置为-1
                    scope.currentTab = -1;
                };
                scope.sheetConfirm = function() {
                    closeSheet()
                        // 通过当前选项判断选择了哪个
                    var type = scope.sheetObj.type
                    var oldValue = scope.sheetObj.oldValue
                    var sheetObj = scope.sheetObj[type]
                    sheetObj.selectTab = sheetObj.tabList[scope.currentTab] && sheetObj.tabList[scope.currentTab].value;//要传的参数
                    sheetObj.selectTabContent = sheetObj.tabList[scope.currentTab] && sheetObj.tabList[scope.currentTab].key;//要显示的参数
                    if (sheetObj.callback && typeof scope.currentTab != "undefined"&&scope.currentTab!=-1) {
                        sheetObj.callback({
                            "oldValue": oldValue,
                            "selectTab": sheetObj.selectTab,
                            "selectTabContent": sheetObj.selectTabContent
                        })
                    }
                    scope.currentTab = -1;
                };
                var closeSheet = function() {
                    scope.sheetObj.ifshowSheet = false
                    $rootScope.showMasker = false
                };


                // element.text('this is the sheetAction directive');
            }
        };
    }]);
